﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Jidong
{
    public partial class 设备入库_In : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int BillID = 0;
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out BillID);
                }
                if (BillID > 0)
                {
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText = @"select 编号,入库日期,供货单位ID,部门单位ID,设备名称,规格,单位
                    ,数量,设备入库单.单价,合同号,出厂编号,质保书及合格证,生产单位,
                    出厂日期,重量,备注,附属设备名称,附属设备规格 from 设备入库单
                    inner join 设备 on 设备入库单.设备类别ID=设备.ID where [设备入库单].[ID]=" + BillID;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {

                //        this.ShiYongDanWei.DataBind();
                //        this.LingYongZhuGuan.DataBind();
                //        this.LingYongRen.DataBind();
                //        this.CangChuZhuGuan.DataBind();
                //        this.KuGuanYuan.DataBind();
                         
                          this.ViewState["BillID"] = BillID;
                          
                          this.DanHao.Text = Reader.GetString(0).Trim();
                          this.DanHao.ReadOnly = true;
                          this.RiQi.Text = Reader.GetDateTime(1).ToString("yyyy年M月d日");

                         
                          //供货单位          
                          if (Reader.IsDBNull(2))
                          {
                              ((DropDownList)this.FindControl("GongHuoDanWei")).Text = "";
                          }
                          else
                          { 
                              string temp = Reader.GetInt32(2).ToString();
                              ((DropDownList)this.FindControl("GongHuoDanWei")).Visible = true;
                              this.GongHuoDanWei.SelectedValue= temp;
                          }
                          //部门
                          if (Reader.IsDBNull(3))
                          {
                              ((DropDownList)this.FindControl("BuMen")).Text = "";
                          }
                          else
                          {
                              string temp1 = Reader.GetInt32(3).ToString();
                              ((DropDownList)this.FindControl("BuMen")).Visible = true;
                              this.BuMen.SelectedValue = temp1;
                          }
                          
                        //设备名称
                          if (Reader.IsDBNull(4))
                          {
                              this.R1_C1.Text = "";
                          }
                          else
                          {
                              this.R1_C1.Text = Reader.GetString(4).Trim();
                          }
                        //规格及型号
                          if (Reader.IsDBNull(5))
                          {
                              this.R1_C2.Text = "";
                          }
                          else
                          {
                              this.R1_C2.Text = Reader.GetString(5).Trim();
                          }
                        //单位
                          if (Reader.IsDBNull(6))
                          {
                              this.R1_C3.Text = "";
                          }
                          else
                          {
                              this.R1_C3.Text = Reader.GetString(6).Trim();
                          }
                        //数量
                          if (Reader.IsDBNull(7))
                          {
                              this.TextBox1.Text = "";
                          }
                          else
                          {  
                              this.TextBox1.CssClass = "intxt1";
                              this.TextBox1.ReadOnly = false;
                              this.TextBox1.Text = Reader.GetInt32(7).ToString();                             
                          }
                        //单价
                          if (Reader.IsDBNull(8))
                          {
                              this.TextBox2.Text = "";
                          }
                          else
                          {
                              this.TextBox2.CssClass = "intxt1";
                              this.TextBox2.ReadOnly = false;
                              this.TextBox2.Text = Reader.GetFloat(8).ToString();
                          }
                        //合同号
                          if (Reader.IsDBNull(9))
                          {
                              this.TextBox3.Text = "";
                          }
                          else
                          {
                              this.TextBox3.CssClass = "intxt1";
                              this.TextBox3.ReadOnly = false;
                              this.TextBox3.Text = Reader.GetString(9).ToString();
                          }
                         //出厂编号
                          if (Reader.IsDBNull(10))
                          {
                              this.R1_C6.Text = "";
                          }
                          else
                          {
                              this.R1_C6.Text = Reader.GetString(10).Trim();
                          }
                          //质保书及合格证
                          if (Reader.IsDBNull(11))
                          {
                              this.R1_C5.Text = "";
                          }
                          else
                          {
                              this.R1_C5.Text = Reader.GetString(11).Trim();
                          }
                          //生产单位
                          if (Reader.IsDBNull(12))
                          {
                              this.TextBox4.Text = "";
                          }
                          else
                          {
                              this.TextBox4.CssClass = "intxt1";
                              this.TextBox4.ReadOnly = false;
                              this.TextBox4.Text = Reader.GetString(12).ToString();
                          }
                        //出厂日期
                          if (Reader.IsDBNull(13))
                          {
                              this.R1_C7.Text = "";
                          }
                          else
                          {
                              this.R1_C7.Text = Reader.GetString(13).Trim();
                          }
                        //重量
                          if (Reader.IsDBNull(14))
                          {
                              this.R1_C4.Text = "";
                          }
                          else
                          {                             
                              this.R1_C4.Text = Reader.GetDouble(14).ToString();
                          }
                        //备注
                          if (Reader.IsDBNull(15))
                          {
                              this.TextBox7.Text = "";
                          }
                          else
                          {
                              this.TextBox7.CssClass = "intxt1";
                              this.TextBox7.ReadOnly = false;
                              this.TextBox7.Text = Reader.GetString(15).Trim();
                          }
                          if (Reader.IsDBNull(16))
                          {
                              this.TextBox5.Text = "";
                          }
                          else
                          {
                              this.TextBox5.CssClass = "intxt1";
                              this.TextBox5.ReadOnly = false;
                              this.TextBox5.Text = Reader.GetString(16).Trim();
                          }
                          if (Reader.IsDBNull(17))
                          {
                              this.TextBox6.Text = "";
                          }
                          else
                          {
                              this.TextBox6.CssClass = "intxt1";
                              this.TextBox6.ReadOnly = false;
                              this.TextBox6.Text = Reader.GetString(17).Trim();
                          }
                          int Amount = 0;
                          float Price = 0;
                          Amount = Reader.GetInt32(7);
                          Price = Reader.GetFloat(8);
                          ((TextBox)this.FindControl("TextBox8")).Text = (Amount * Price).ToString("0.00");
                //        temp = Reader.GetString(6).Trim();
                //        if (this.KuGuanYuan.Items.FindByValue(temp) != null)
                //        {
                //            this.KuGuanYuan.Text = temp;
                //        }

                //        temp = Reader.GetString(3).Trim();
                //        if (this.LingYongZhuGuan.Items.FindByValue(temp) != null)
                //        {
                //            this.LingYongZhuGuan.Text = temp;
                //        }

                //        temp = Reader.GetString(4).Trim();
                //        if (this.LingYongRen.Items.FindByValue(temp) != null)
                //        {
                //            this.LingYongRen.Text = temp;
                //        }

                //        this.BeiZhu.Text = Reader.GetString(7).Trim();

                //        Reader.Close();
                //        Cmd.CommandText = "select [备品备件库存].[类别ID] ,[备品备件].[品名],[备品备件].[规格],[备品备件].[单位],[备品备件库存].[数量],[备品备件库存].[单价] from [备品备件库存],[备品备件] where [备品备件库存].[类别ID] = [备品备件].[id] and [备品备件库存].[数量]<0 and [备品备件库存].[单据ID]=" + BillID;
                //        Reader = Cmd.ExecuteReader();
                //        for (int i = 1; i <= 4; i++)
                //        {
                //            if (!Reader.Read())
                //            {
                //                break;
                //            }
                //            this.ViewState["id" + i] = Reader.GetInt32(0).ToString();
                //            ((TextBox)this.FindControl("R" + i + "_C1")).Text = Reader.GetString(1);
                //            ((TextBox)this.FindControl("R" + i + "_C2")).Text = Reader.GetString(2);
                //            ((TextBox)this.FindControl("R" + i + "_C3")).Text = Reader.GetString(3);
                              
                //            ((TextBox)this.FindControl("R" + i + "_C4")).Text = Amount.ToString();
                //            ((TextBox)this.FindControl("R" + i + "_C5")).Text = Price.ToString();
                           
                //            ((TextBox)this.FindControl("R" + i + "_C4")).ReadOnly = false;
                //            ((TextBox)this.FindControl("R" + i + "_C4")).CssClass = "intxt1";
                //            ((TextBox)this.FindControl("R" + i + "_C5")).ReadOnly = false;
                //            ((TextBox)this.FindControl("R" + i + "_C5")).CssClass = "intxt1";
                //            ((Button)this.FindControl("Button_Add" + i)).Text = "删 除";
                //        }
                //        Reader.Close();
                //    }
                //    else
                //    {
                //        Reader.Close();
                //        this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                   }

                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
                else
                {
                    this.RiQi.Text = DateTime.Today.ToString("yyyy年M月d日");
                }
            }
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void ImageButton_Cancel_Click(object sender, ImageClickEventArgs e)
        {
            this.Panel_Grid.Visible = false;
            this.Message.Text = "·已取消添加";
        }

        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {
            int row;
            int.TryParse(e.CommandArgument.ToString(), out row);
            this.ViewState["row"] = row;

            if (this.ViewState["id"+row] == null)
            {
                this.Panel_Grid.Visible = true;
                this.Message.Text = "·准备添加";
            }
            else
            {
                this.ViewState["id" + row] = null;
                ((TextBox)this.FindControl("R" + row + "_C1")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C2")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C3")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C4")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C5")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C6")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C7")).Text = "";
                ((TextBox)this.FindControl("TextBox2")).Text = "";
                ((TextBox)this.FindControl("R" + row + "_C1")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C1")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C2")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C2")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C3")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C3")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C4")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C4")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C5")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C5")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C6")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C6")).CssClass = "blank_center";
                ((TextBox)this.FindControl("R" + row + "_C7")).ReadOnly = true;
                ((TextBox)this.FindControl("R" + row + "_C7")).CssClass = "blank_center";
                for (int i = 1; i <=7; i++)
                {
                    ((TextBox)this.FindControl("TextBox" + i)).ReadOnly = true;
                    ((TextBox)this.FindControl("TextBox" + i)).CssClass = "blank_center";
                }
                this.GongHuoDanWei.Visible = false;
                this.BuMen.Visible = false;
               ((Button)this.FindControl("Button_Add")).Text = "添 加";
                this.Message.Text = "·删除成功";
            }

        }

        protected void Button1_Command(object sender, CommandEventArgs e)
        {
            int row = (int)this.ViewState["row"];
            string[] Args = e.CommandArgument.ToString().Split("\t".ToCharArray());

            this.ViewState["id" + row] = Args[0];
            ((TextBox)this.FindControl("R" + row + "_C1")).Text = Args[1];
            ((TextBox)this.FindControl("R" + row + "_C2")).Text = Args[2];
            ((TextBox)this.FindControl("R" + row + "_C3")).Text = Args[3];
            ((TextBox)this.FindControl("R" + row + "_C4")).Text = Args[4];
            ((TextBox)this.FindControl("TextBox2")).Text = Args[5];
            ((TextBox)this.FindControl("R" + row + "_C5")).Text = Args[6];
            ((TextBox)this.FindControl("R" + row + "_C6")).Text = Args[7];
            ((TextBox)this.FindControl("R" + row + "_C7")).Text = Args[8];
           
            for (int i = 1; i <= 7; i++)
            {
                ((TextBox)this.FindControl("TextBox" + i)).ReadOnly = false;
                ((TextBox)this.FindControl("TextBox" + i)).CssClass = "intxt1";
            }
            this.GongHuoDanWei.Visible = true;
            this.BuMen.Visible = true;
            ((Button)this.FindControl("Button_Add")).Text = "删 除";
            this.Panel_Grid.Visible = false;
            this.Message.Text = "·添加成功";
        }

        protected void RowData_TextChanged(object sender, EventArgs e)
        {
            Response.Write(((TextBox)sender).ID);
        }

        protected void Button_Calc_Click(object sender, EventArgs e)
        {
            int f1;
            float f2;
            this.Message.Text = "";
             f1 = 0;
             f2 = 0;
             int.TryParse(((TextBox)this.FindControl("TextBox1")).Text, out f1);
             float.TryParse(((TextBox)this.FindControl("TextBox2")).Text, out f2);
             if (f1 * f2 <= 0)
              {
                ((TextBox)this.FindControl("TextBox8")).Text = "";
                 this.Message.Text += " ·第" + 1 + "行数量或者单价输入有误";
              }
             else
              {
                 ((TextBox)this.FindControl("TextBox8")).Text = (f1 * f2).ToString(".00");
              }             
            if (this.Message.Text == "")
            {
                this.Message.Text = "·完成金额计算";
            }
        }

        protected void Button_Finish_Click(object sender, EventArgs e)
        {
            string i=Request.QueryString["id"];  
            if (this.GongHuoDanWei.Visible==false)
            {
                this.Message.Text = "·请先添加设备在保存"; return;
            }
            if (i!=null)
            {
                Button_Calc_Click(null, null);
                if (this.Message.Text != "·完成金额计算") return;
            }
          
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlTransaction Tran = Conn.BeginTransaction();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.Transaction = Tran;
            try
            {
                Cmd.CommandText = "设备_In_Bill";
                Cmd.CommandType = CommandType.StoredProcedure;
                if (this.ViewState["BillID"] == null)
                {
                    this.ViewState["BillID"] = -1;
                }
                Cmd.Parameters.Add(new SqlParameter("@编号", SqlDbType.Char, 10, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.DanHao.Text));
                Cmd.Parameters.Add(new SqlParameter("@入库日期", SqlDbType.Date, 3, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, DateTime.Parse(RiQi.Text)));
                Cmd.Parameters.Add(new SqlParameter("@供货单位ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.GongHuoDanWei.Text)));
                Cmd.Parameters.Add(new SqlParameter("@部门单位ID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, int.Parse(this.BuMen.Text)));
                Cmd.Parameters.Add(new SqlParameter("@设备类别ID", SqlDbType.Real, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                Cmd.Parameters.Add(new SqlParameter("@数量", SqlDbType.Real, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                Cmd.Parameters.Add(new SqlParameter("@单价", SqlDbType.Real, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, 0));
                Cmd.Parameters.Add(new SqlParameter("@合同号", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed,this.TextBox3.Text));
                Cmd.Parameters.Add(new SqlParameter("@生产单位", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed,this.TextBox4.Text));
                Cmd.Parameters.Add(new SqlParameter("@附属设备名称", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.TextBox5.Text));
                Cmd.Parameters.Add(new SqlParameter("@附属设备规格", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.TextBox6.Text));
                //Cmd.Parameters.Add(new SqlParameter("@领用主管", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.LingYongZhuGuan.Text));
                //Cmd.Parameters.Add(new SqlParameter("@领用人", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.LingYongRen.Text));
                //Cmd.Parameters.Add(new SqlParameter("@仓储主管", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.CangChuZhuGuan.Text));
                //Cmd.Parameters.Add(new SqlParameter("@库管员", SqlDbType.NChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.KuGuanYuan.Text));
                Cmd.Parameters.Add(new SqlParameter("@备注", SqlDbType.NVarChar, 400, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.TextBox7.Text));
                Cmd.Parameters.Add(new SqlParameter("@BillID", SqlDbType.Int, 4, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ViewState["BillID"]));
                Cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Proposed, null));
                if ((string)ViewState["id" + 1] != null)
                {
                    Cmd.Parameters["@设备类别ID"].Value = int.Parse((string)ViewState["id" + 1]);
                    Cmd.Parameters["@数量"].Value = float.Parse(((TextBox)this.FindControl("TextBox1")).Text);
                    Cmd.Parameters["@单价"].Value = float.Parse(((TextBox)this.FindControl("TextBox2")).Text);
                }

                Cmd.ExecuteNonQuery();
              
                this.ViewState["BillID"] = (int)Cmd.Parameters["@ID"].Value;
                int BillID = (int)this.ViewState["BillID"];
                if (BillID > 0)
                {
                    if (i != null)
                    {
                        Cmd.CommandText = "设备_In_Bill";
                        Cmd.CommandType = CommandType.StoredProcedure;
                        Cmd.Parameters["@设备类别ID"].Value =int.Parse(Request.QueryString["SID"]);
                        Cmd.Parameters["@数量"].Value = int.Parse(((TextBox)this.FindControl("TextBox1")).Text);
                        Cmd.Parameters["@单价"].Value = float.Parse(((TextBox)this.FindControl("TextBox2")).Text);
                     }
                        this.Message.Text = "·保存成功";
                        this.DanHao.ReadOnly = true;
                        Cmd.ExecuteNonQuery();
                    
               }
                else
                {
                    this.Message.Text = "·单价编号(No.)已经存在";
                }
               
                    Tran.Commit();
            }
            catch (Exception err)
            {
                Tran.Rollback();
                this.Message.Text=err.Message;
            }
            Cmd.Dispose();
            Tran.Dispose();
            Conn.Close();
            Conn.Dispose();
        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            Response.Redirect("设备入库_In");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("设备入库_In_Bill.aspx");
        }

        protected void Button_Add_Click(object sender, EventArgs e)
        {

        }

       

      

       

     

       

        

        /////////////////////
    }
}
